/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.util.Random;
import javax.microedition.lcdui.Image;


/**
 *
 * @author HuuTai_it
 */
public class RedShip extends Ship {
    
    public RedShip (GameManagement gameMng, Image image) throws Exception {
        
        super(gameMng, image);
        
        Random r = new Random();
        boolean temp = true;
        while(temp) {
            
            int x = Math.abs(r.nextInt() % 10);
            int y = Math.abs(r.nextInt() % 10);
            int mX = gameMng.mainShip.getX() / 24;
            int mY = gameMng.mainShip.getY() / 24;
            
            if(gameMng.element[x][y] == 0 &&
               ((gameMng.control.level < 6 && (Math.abs(x - mX) > 3 || Math.abs(y - mY) > 3)) ||
               (gameMng.control.level > 5 && gameMng.control.level < 11 && (Math.abs(x - mX) > 2 || Math.abs(y - mY) > 2)) ||
               (gameMng.control.level > 10 && (Math.abs(x - mX) > 1 || Math.abs(y - mY) > 1)))) {
                
                setPosition(x * 24, y * 24);
                gameMng.element[x][y] = 6;
                temp = false;
            }
        }
    }
    public void autoMove(Ship s, int oldx, int oldy){
        //Neu va cha.m ngay
        
        Random r = new Random();
        if(Math.abs(s.getX() - getX()) <= 24 && Math.abs(s.getY() - getY()) <= 24)
        {//kill
            super.autoMove(s, oldx, oldy);
            return;
        }
        int nextX = getX();
        int nextY = getY();
        int dir[] = {0, 0, 0};
        while((control.g.element[nextX/24][nextY/24]!= 0 || (nextX == getX() && nextY == getY()))
                && (dir[0]* dir[1] * dir[2] == 0))
        {
            nextX = getX();
            nextY = getY();
            int randir  = 0;
            do
            {
            randir =  Math.abs(r.nextInt() % 3) ;
            }
            //di 1 trong 3 huong
            while (dir[randir] == 1 && (dir[0]* dir[1] * dir[2] == 0));
            dir[randir] = 1;
            if(getX() < s.getX() )
                {
                    if(getY() < s.getY())
                    {
                        nextX = dirc[5 - randir][0];
                        nextY = dirc[5 - randir][1];
                    }
                    else if(getY() > s.getY())
                    {
                        nextX = dirc[3- randir][0];
                        nextY = dirc[3- randir][1];
                    }
                    else
                        nextX = dirc[4- randir][0];
                        nextY = dirc[4- randir][1];
                }else
                    if(getX() > s.getX())
                    {
                        if(getY() < s.getY())
                        {
                            nextX = dirc[7- randir][0];
                            nextY = dirc[7- randir][1];
                        }
                        else if(getY() > s.getY())
                        {
                            nextX = dirc[9- randir][0];
                            nextY = dirc[9- randir][1];
                        }
                        else
                            nextX = dirc[8- randir][0];
                            nextY = dirc[8- randir][1];                                          
                    }
                else
                   if(getY() < s.getY())
                    {
                        nextX = dirc[6- randir][0];
                        nextY = dirc[6- randir][1];
                    }
                    else if(getY() > s.getY())
                    {
                        nextX = dirc[2- randir][0];
                        nextY = dirc[2- randir][1];
                    } 
                    break;
        }
        // huong bi chan => di random
        while((control.g.element[nextX/24][nextY/24]!= 0))
        {
            int ran =  Math.abs(r.nextInt() % 8) ;
            nextX = getX();
            nextY = getY();
            nextX = dirc[ran][0];
            nextY = dirc[ran][1];
        }
    }
}

